package com.wisdudu.blank.ui.user;

import android.databinding.ObservableField;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextListener;
import com.wisdudu.blank.support.rxbus.RxBus;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;

/**
 * Created by ypp on 2016/8/29.
 */
public class UserNickEditVM implements ViewModel {
    public ObservableField<String> nickName = new ObservableField<>();
    private UserRepo userRepo;
    private UserNickEditFragment mFragment;
    public final ObservableField<Integer> etLength = new ObservableField<>();

    public UserNickEditVM(UserNickEditFragment userNickEditFragment, String string) {
        userRepo = Injection.provideUserRepo();
        this.mFragment = userNickEditFragment;
        nickName.set(string);
        etLength.set(12);
    }

    public void confirm() {
        if (TextUtils.isEmpty(nickName.get())) {
            ToastUtil.INSTANCE.toast("昵称不能为空！");
            return;
        }
        userRepo.editNickname(nickName.get()).compose(mFragment.bindToLifecycle()).subscribe(new ProgressSubscriber<Object>(new SubscriberOnNextListener<Object>() {
            public void onNext(Object o, LoadingProgressDialog pd) {
                ToastUtil.INSTANCE.toast("修改成功");
                RxBus.getDefault().post(nickName.get());
                mFragment.removeFragment();
            }
        }, mFragment.mActivity, "正在修改...", 1000));
    }


}